package com.example.wufan.spinnwetest425wf;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView tvinfo;
    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        //定义数据源----1
        String[] cities={"北京","上海","天津","大连"};
        //实例化ArrayAdapter
        final ArrayAdapter<String> adpater=new ArrayAdapter<String>(
                this,
                android.R.layout.simple_spinner_item,
                cities
        );
        //设置下拉按钮样式
        adpater.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
        //将spinner与ArrayAdapter绑定
        spinner.setAdapter(adpater);
        spinner.setPrompt("请选择城市");//本句为Spinner是对话框形式而设的标题
        //设置spinner某条目被点击时响应
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                              @Override
                                              public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                                                  tvinfo.setText("您选择的是:"
                                                          +adapterView.getItemAtPosition(position).toString());
                                              }

                                              @Override
                                              public void onNothingSelected(AdapterView<?> adapterView) {

                                              }
        });
    }


    private void init() {
        tvinfo=(TextView)findViewById(R.id.TestView);
        spinner=(Spinner)findViewById(R.id.spinner);
    }
}

